package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.behavior.vo.Behaviorvo;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class ApArticleBehaviorimpl implements ApArticleBehaviorService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //new data对象
        Behaviorvo behaviorvo = new Behaviorvo();
        //校验参数 登录
        ApUser user = AppThreadLocalUtils.getUser();
        //判断参数,是否登录
        if (user == null) {
            behaviorvo.setIscollection(false);
            behaviorvo.setIsfollow(false);
            behaviorvo.setIslike(false);
            behaviorvo.setIsunlike(false);
            return ResponseResult.okResult(behaviorvo);
        }
        //根据条件查询,
        ApBehaviorEntry userIdOrEquipmentId = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        String id = userIdOrEquipmentId.getId();
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId()));

        //是否点赞
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (apLikesBehavior != null) {
            behaviorvo.setIslike(true);
        }
        //是否喜欢
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if (apUnlikesBehavior != null) {
            behaviorvo.setIsunlike(true);
        }
        //是否收藏
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if (apCollection != null) {
            behaviorvo.setIscollection(true);
        }
        Integer followId = dto.getAuthorApUserId();
        Double score = redisTemplate.opsForZSet()
                .score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(followId));
        if (score != null) {
            behaviorvo.setIsfollow(true);
        }

        //
        return ResponseResult.okResult(behaviorvo);
    }
}
